package com.cl.ks;

import com.cl.ks.service.KsRuleService;
import com.cl.ks.utils.DbUtil;
import com.cl.spark.util.SparkUtil;
import com.cl.ks.repository.ClueAutoResultRepository;
import com.cl.ks.schedule.KsFlowTaskStarter;
import com.cl.ks.service.AutoFilterService;
import com.cl.ks.service.FilterService;
import com.cl.ks.service.SparkNodeService;
import com.querydsl.jpa.impl.JPAQueryFactory;
import com.ulisesbocchio.jasyptspringboot.annotation.EnableEncryptableProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @author chenyitCo * @date 2021/4/19
 */
@EnableSwagger2
@SpringBootApplication(scanBasePackages = {"com.cl.ks","com.cl.spark","com.cl.pile","com.cl.schedule"})
@EnableEncryptableProperties
@EnableJpaRepositories
public class Application implements CommandLineRunner {

    @Autowired
    JPAQueryFactory queryFactory;
    @Autowired
    AutoFilterService autoFilterService;
    @Autowired
    FilterService filterService;
    @Autowired
    KsRuleService ksRuleService;

    @Autowired
    KsFlowTaskStarter ksFlowTaskStarter;

    @Autowired
    SparkNodeService sparkNodeService;
    @Autowired
    ClueAutoResultRepository clueAutoResultRepository;
    @Autowired
    SparkUtil sparkUtil;
    @Autowired
    DbUtil dbUtil;

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Override
    public void run(String... args)  {
//        sparkUtil.test();
//        BooleanBuilder booleanBuilder = new BooleanBuilder();
//        booleanBuilder.and(qKsRule.id.gt(200));
//        booleanBuilder.and(qKsRule.ruleType.contains("刑事"));
//        List<KsRule> fetch = queryFactory.selectFrom(qKsRule).where(booleanBuilder).fetch();
//        System.out.println();
//        log.info("111");
//        Document document = Jsoup.parse(new File("/Users/chenlong/Downloads/111.html"));
//        String text = document.text();
//        String[] s = text.split(" ");
//        log.info("222");

//        sparkNodeService.allFilterToKsFlowGraph();
//        ksFlowTaskStarter.startAll();
//        autoFilterService.autoUpdate();
//        List<KsRule> list = ksRuleService.list();
//        for (KsRule ksRule : list) {
////            KsRule ksRule = ksRuleService.findById(10001928).get();
//            String ruleExpressionJson = ksRule.getRuleExpressionJson();
//            JSONObject jsonObject = new JSONObject(ruleExpressionJson);
//            updateOpBasedOnTips(jsonObject);
//            ksRule.setRuleExpressionJson(jsonObject.toString(2));
//            ksRuleService.save(ksRule);
//        }
    }
//    public static void updateOpBasedOnTips(JSONObject node) {
//        if (node.has("tips")) {
//            String tips = node.optString("tips");
//            String op = node.optString("op");
//            if (tips.contains("高于") && op.contains(">=")) {
//                node.put("op", ">");
//            } else if (tips.contains("低于") && op.contains("<=")) {
//                node.put("op", "<");
//            }
//        }
//        // 递归处理子节点
//        if (node.has("and")) {
//            JSONArray andArray = node.getJSONArray("and");
//            for (int i = 0; i < andArray.length(); i++) {
//                updateOpBasedOnTips(andArray.getJSONObject(i));
//            }
//        } else if (node.has("or")) {
//            JSONArray orArray = node.getJSONArray("or");
//            for (int i = 0; i < orArray.length(); i++) {
//                updateOpBasedOnTips(orArray.getJSONObject(i));
//            }
//        } else if (node.has("if")) {
//            JSONObject ifNode = node.getJSONObject("if");
//            if (ifNode.has("condition")) {
//                updateOpBasedOnTips(ifNode.getJSONObject("condition"));
//            }
//            if (ifNode.has("true")) {
//                updateOpBasedOnTips(ifNode.getJSONObject("true"));
//            }
//            if (ifNode.has("false")) {
//                updateOpBasedOnTips(ifNode.getJSONObject("false"));
//            }
//        }
//    }



}
